package com.gitee.huanminabc.jcommon.container;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 各种常用容器的快速创建
 *
 * @author huanmin
 * @date 2024/1/19
 */
public class ContainerCreate {

    //自动根据类型创建
    public static Object autoCreate(Class<?> c) {
        if (c == HashSet.class) {
            return new HashSet<>();
        } else if (c == ArrayList.class) {
            return new ArrayList<>();
        } else if (c == LinkedList.class) {
            return new LinkedList<>();
        } else if (c == LinkedHashSet.class) {
            return new LinkedHashSet<>();
        } else if (c == TreeSet.class) {
            return new TreeSet<>();
        } else if (c == LinkedHashMap.class) {
            return new LinkedHashMap<>();
        } else if (c == ConcurrentHashMap.class) {
            return new ConcurrentHashMap<>();
        } else if (c == HashMap.class) {
            return new HashMap<>();
        } else if (c == TreeMap.class) {
            return new TreeMap<>();
        } else if (c == Map.class) {
            return new HashMap<>();
        } else if (c == List.class) {
            return new ArrayList<>();
        } else if (c == Set.class) {
            return new HashSet<>();
        }
        return null;
    }


    public static <T> List<T> newList(T... elements) {
        return new ArrayList<>(Arrays.asList(elements));
    }

    public static <T> List<T> newLkdList(T... elements) {
        return new LinkedList<>(Arrays.asList(elements));
    }

    public static <T> Set<T> newSetList(T... elements) {
        return new HashSet<>(Arrays.asList(elements));
    }

    public static <T> Set<T> newLkdSetList(T... elements) {
        return new LinkedHashSet<>(Arrays.asList(elements));
    }

    public static <T> Set<T> newTreeSetList(T... elements) {
        return new TreeSet<>(Arrays.asList(elements));
    }

    public static <K, V> Map<K, V> newMap() {
        return new HashMap<>();
    }

    public static <K, V> Map<K, V> newLkdMap() {
        return new LinkedHashMap<>();
    }

    public static <K, V> Map<K, V> newTreeMap() {
        return new TreeMap<>();
    }

    public static <K, V> Map<K, V> newMap(K k, V v) {
        HashMap<K, V> kvHashMap = new HashMap<>();
        kvHashMap.put(k, v);
        return kvHashMap;
    }

    public static <K, V> Map<K, V> newLkdMap(K k, V v) {
        LinkedHashMap<K, V> kvLinkedHashMap = new LinkedHashMap<>();
        kvLinkedHashMap.put(k, v);
        return kvLinkedHashMap;

    }

    public static <K, V> Map<K, V> newTreeMap(K k, V v) {
        TreeMap<K, V> kvTreeMap = new TreeMap<>();
        kvTreeMap.put(k, v);
        return kvTreeMap;
    }


    /**
     * 队列创建 ,比LinkedList作为队列快,非阻塞,非线程安全
     *
     * @param <T>
     * @return
     */
    public static <T> Queue<T> newQueue() {
        return new ArrayDeque<>();
    }

    //阻塞队列,线程安全,无界 ,int最大值
    public static <T> Queue<T> newLkdBlockingQueue() {
        return new LinkedBlockingQueue<>();
    }

    //阻塞队列,线程安全,有界
    public static <T> Queue<T> newLkdBlockingQueue(int size) {
        return new LinkedBlockingQueue<>(size);
    }
}
